﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Xps.Packaging;
using CodeReason.Reports;
using System.Diagnostics;
using report_demo.Data;

namespace report_demo {
    /// <summary>
    /// Interaction logic for ReportWindow.xaml
    /// </summary>
    public partial class ReportWindow : Window {
        
        private String templatePath = "c:\\void\\report_templates\\";//read from configuration file

        private ReportData _reportData;
        private ReportDocument _reportDocument = new ReportDocument();
        public ReportWindow() {
            InitializeComponent();
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="templateName"></param>
        private void loadTemplate(String templateName) {
            _reportDocument = new ReportDocument();

            Debug.Write(Environment.CurrentDirectory);

            StreamReader reader = new StreamReader(new FileStream(templatePath + templateName, FileMode.Open, FileAccess.Read));
            _reportDocument.XamlData = reader.ReadToEnd();
            _reportDocument.XamlImagePath = Path.Combine(Environment.CurrentDirectory, templatePath);
            reader.Close();
            //ReportData 
            _reportData = new ReportData();
            // set constant document values
            _reportData.ReportDocumentValues.Add("PrintDate", DateTime.Now); // print date is now
        }
        #region Form 40
        /// <summary>
        /// 
        /// </summary>
        public static void showForm40() {
            ReportWindow form = new ReportWindow();
            form.generateForm40();
            form.Show();
        }
        /// <summary>
        /// 
        /// </summary>
        private void generateForm40() {
            try {
                loadTemplate("Form40Report.xml");
                //
                DataTable table = new DataTable("report-demo");
                foreach (String cn in CustomerData.getInstance().getColumnNameArray()) {
                    table.Columns.Add(cn, typeof(string));
                }
                for (int i = 0; i < 17; i++) {
                    table.Rows.Add(new object());
                }
                _reportData.DataTables.Add(table);
                //
                DateTime dateTimeStart = DateTime.Now; // start time measure here
                XpsDocument xps = _reportDocument.CreateXpsDocument(_reportData);
                documentViewer.Document = xps.GetFixedDocumentSequence();
                Title += " - generated in " + (DateTime.Now - dateTimeStart).TotalMilliseconds + "ms";
            } catch (Exception ex) {
                // show exception
                MessageBox.Show(ex.Message + "\r\n\r\n" + ex.GetType() + "\r\n" + ex.StackTrace, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop);
            } finally {
                //busyDecorator.IsBusyIndicatorHidden = true;
            }
        }
        #endregion

        #region Form 39
        /// <summary>
        /// 
        /// </summary>
        public static void showForm39() {
            ReportWindow form = new ReportWindow();
            form.generateForm39();
            form.Show();
        }
        /// <summary>
        /// 
        /// </summary>
        private void generateForm39() {
            try {
                loadTemplate("Form39Report.xml");
                //
                DataTable table = new DataTable("report-demo");
                foreach (String cn in CustomerData.getInstance().getColumnNameArray()) {
                    table.Columns.Add(cn, typeof(string));
                }
                for (int i = 0; i < 17; i++) {
                    table.Rows.Add(new object());
                }
                _reportData.DataTables.Add(table);
                //
                DateTime dateTimeStart = DateTime.Now; // start time measure here
                XpsDocument xps = _reportDocument.CreateXpsDocument(_reportData);
                documentViewer.Document = xps.GetFixedDocumentSequence();
                Title += " - generated in " + (DateTime.Now - dateTimeStart).TotalMilliseconds + "ms";
            } catch (Exception ex) {
                // show exception
                MessageBox.Show(ex.Message + "\r\n\r\n" + ex.GetType() + "\r\n" + ex.StackTrace, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop);
            } finally {
                //busyDecorator.IsBusyIndicatorHidden = true;
            }
        }
        #endregion

        #region Form41p1
        /// <summary>
        /// 
        /// </summary>
        public static void showForm41p1() {
            ReportWindow form = new ReportWindow();
            form.generateForm41p1();
            form.Show();
        }
        /// <summary>
        /// 
        /// </summary>
        private void generateForm41p1() {
            try {
                loadTemplate("Form41P1Report.xml");
                //
                DataTable table = new DataTable("report-demo");
                foreach (String cn in CustomerData.getInstance().getColumnNameArray()) {
                    table.Columns.Add(cn, typeof(string));
                }
                for (int i = 0; i < 17; i++) {
                    table.Rows.Add(new object());
                }
                _reportData.DataTables.Add(table);
                //
                DateTime dateTimeStart = DateTime.Now; // start time measure here
                XpsDocument xps = _reportDocument.CreateXpsDocument(_reportData);
                documentViewer.Document = xps.GetFixedDocumentSequence();
                Title += " - generated in " + (DateTime.Now - dateTimeStart).TotalMilliseconds + "ms";
            } catch (Exception ex) {
                // show exception
                MessageBox.Show(ex.Message + "\r\n\r\n" + ex.GetType() + "\r\n" + ex.StackTrace, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop);
            } finally {
                //busyDecorator.IsBusyIndicatorHidden = true;
            }
        }
        #endregion

        
    }
}